package com.lovo.invoicing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.invoicing.bean.BuyCartBean;
import com.lovo.invoicing.bean.SaleCartBean;
import com.lovo.invoicing.bean.vo.OutGoodsBean;
import com.lovo.invoicing.mapper.SaleCartMapper;
import com.lovo.invoicing.service.IGoodsService;
import com.lovo.invoicing.service.ISaleCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SaleCartServiceImpl extends ServiceImpl<SaleCartMapper, SaleCartBean> implements ISaleCartService {
    @Autowired
    SaleCartMapper saleCartMapper;
    @Autowired
    IGoodsService goodsService;

    @Override
    @Transactional
    public int addIntoCart(OutGoodsBean outGoodsBean,int userId) {
        int inventoryNo = goodsService.queryGoodsByGoodsNo(outGoodsBean.getGoodsNo()).getInventoryNo();
        QueryWrapper qw1 = new QueryWrapper<>();
        qw1.eq("goods_no",outGoodsBean.getGoodsNo());
        qw1.eq("fk_userid",userId);
        SaleCartBean saleCartBean = saleCartMapper.selectOne(qw1);
        if (saleCartBean!=null) {
            int saleNo = saleCartBean.getSaleNo();
            if (saleNo == inventoryNo) {
                return 0;
            }
        }
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("fk_userid",userId);
        List<SaleCartBean> list = baseMapper.selectList(qw);
        if (list.size()>0) {
            for (SaleCartBean c : list) {
                if (c.getGoodsNo().equals(outGoodsBean.getGoodsNo())) {
                    SaleCartBean bc = baseMapper.selectById(c.getSalecartId());
                    bc.setSaleNo(bc.getSaleNo() + 1);
                    baseMapper.updateById(bc);
                    return 1;
                }
            }
        }
        SaleCartBean s = new SaleCartBean();
        s.setGoodsName(outGoodsBean.getGoodsName());
        s.setGoodsBrand(outGoodsBean.getGoodsBrand());
        s.setSalePrice(outGoodsBean.getSalePrice());
        s.setSaleNo(1);
        s.setGoodsType(outGoodsBean.getGoodsType());
        s.setGoodsNo(outGoodsBean.getGoodsNo());
        saleCartMapper.insertSaleCart(s,userId);
        return 2;
    }

    @Override
    @Transactional
    public int updateSaleCart(SaleCartBean saleCartBean) {
        int inventoryNo = goodsService.queryGoodsByGoodsNo(saleCartBean.getGoodsNo()).getInventoryNo();
        if (saleCartBean.getSaleNo()<=inventoryNo) {
            saleCartMapper.updateSaleCart(saleCartBean);
        }
        return inventoryNo;
    }

    @Override
    @Transactional
    public void deleteSaleCartById(int saleCartId) {
          baseMapper.deleteById(saleCartId);
    }

    @Override
    @Transactional
    public void deleteAllSaleCart(int userId) {
        QueryWrapper qw =new QueryWrapper<>();
        qw.eq("fk_userid",userId);
        baseMapper.delete(qw);
    }

    @Override
    public List<SaleCartBean> querySaleCarts(int userId) {
        QueryWrapper<SaleCartBean> qw = new QueryWrapper<>();
        qw.eq("fk_userid",userId);
        return baseMapper.selectList(qw);
    }
}
